package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品 同时保存菜品口味
     *
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置口味表的dishId
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
        /*List<DishFlavor> list = flavors.stream().map((flavor) -> {
            Long id = dishDto.getId();
            flavor.setDishId(id);
            return flavor;
        }).collect(Collectors.toList());*/
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据菜品id查询菜品及口味
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品信息  删除之前的口味,再添加口味
     *
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
       /* List<DishFlavor> list = flavors.stream().map((flavor) -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());*/
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品信息 同时删除对应的口味
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //删除菜品信息 启售不能删 关联套餐不能删
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getStatus,1);
        lqw.in(Dish::getId,ids);
        int count = count(lqw);
        if (count > 0 ){
            //有启售菜品
            throw new CustomException("存在启售菜品,不能删除");
        }
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getDishId,ids);
        int count1 = setmealDishService.count(lqw2);
        if (count1 > 0){
            //关联套餐
            throw new CustomException("当前菜品关联套餐,不能删除");
        }
        removeByIds(ids);
        //删除对应的口味
        LambdaQueryWrapper<DishFlavor> lqw3 = new LambdaQueryWrapper<>();
        lqw3.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw3);

    }

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<DishDto> pageByName(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        return dishMapper.pageByName(pageInfo,name);
    }

    @Override
    public List<DishDto> listWithFlavor(Dish dish) {
        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
        //启售
        dishLqw.eq(Dish::getStatus, 1);
        //分类id
        dishLqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //模糊查询
        dishLqw.eq(dish.getName()!=null,Dish::getName,dish.getName());
        //排序
        dishLqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = list(dishLqw);

        //将List<Dish>转换为List<DishDto>
        List<DishDto> dtoList = dishList.stream().map(dish1 -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish1, dishDto);
            LambdaQueryWrapper<DishFlavor> flavorLqw = new LambdaQueryWrapper<>();
            flavorLqw.eq(DishFlavor::getDishId, dish1.getId());
            List<DishFlavor> list = dishFlavorService.list(flavorLqw);
            dishDto.setFlavors(list);
            return dishDto;
        }).collect(Collectors.toList());

        return dtoList;
    }
}
